use crate::config::CFG;
use salvo::cors::{AllowHeaders, AllowMethods, AllowOrigin, Cors, CorsHandler};

pub fn cors_middleware() -> CorsHandler {
    let cors_allow_origin = &CFG.server.cors_allow_origin;
    let cors_handler = if cors_allow_origin.contains(&"*".to_string()) {
        tracing::warn!("cors allow origin is *");
        Cors::new().allow_origin(AllowOrigin::any())
    } else {
        Cors::new().allow_origin(&CFG.server.cors_allow_origin)
    }
    .allow_methods(AllowMethods::any())
    .allow_headers(AllowHeaders::any())
    .into_handler();
    cors_handler
}
